<%@ page import="bw.news.*" %>
<%@ page import="bw.theme.*" %>
<%@ page import="bw.util.Log" %>
<%@ page import="bw.util.RefresherException" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>

<%

  NewsManager manager = NewsManager.getInstance();

  String message = "";

  String newsSourceId = request.getParameter( "newsSourceId" );
  if ( newsSourceId != null && !"-2".equals( newsSourceId ) )
  {
    if ( "-1".equals( newsSourceId ) )
    {
      Iterator srcs = manager.getNewsSources();
      while ( srcs.hasNext() ) {
        NewsSource s = (NewsSource) srcs.next();
        try {
	  s.updateSource();
	  message += s.getName() + " successfully updated!<br>";
	} catch ( RefresherException ex ) {
	  Log.getInstance().write( "Problem manually refreshing: " + s.getSrc(), ex );
          message += "Problem updating source: " + s.getName() + "<br>";
        }
      }
    } else {
      NewsSource s = manager.getNewsSourceForId( newsSourceId );
      try {
        s.updateSource();
        message = s.getName() + " successfully updated!";
      } catch ( RefresherException ex ) {
        Log.getInstance().write( "Problem manually refreshing: " + s.getSrc(), ex );
        message = "Problem updating source: " + s.getName();
      }
    }
  }

  if ( "true".equals( request.getParameter( "updateTheme" ) ) ) {
    //read in the theme info from a config file
    try
    {
       
       String filename = application.getRealPath( application.getInitParameter( "theme.conf" ) );
       File themeFile = new File( filename );

       //digest the config file
       FileInputStream in = new FileInputStream( themeFile );

       Properties colors = new Properties();
       colors.load( in );

       ThemeManager.getInstance().init( colors );

       message = "Theme successfully updated!";       
    }
    catch ( Exception ex )
    {
       message = "Problem loading theme! Using defaults!";
    }

  }

  Iterator sources = manager.getNewsSources();
  
%>

<html>
<head>
<title>WebDash Config</title>

<script>

function doUpdateTheme ()
{
  document.forms[0].updateTheme.value = "true";
  document.forms[0].submit();
}  

</script>

</head>
<body>

<p>
<%= message %>
</p>

<p>
Select a news source to manually update.
</p>

<form>
<select name="newsSourceId">
  <option value="-2">Select a News Source</option>
  <option value="-1">All News Sources</option>
<% 
  while ( sources.hasNext() ) {
    NewsSource source = (NewsSource) sources.next();
%>
  <option value="<%= source.getId() %>"><%= source.getName() %></option>
<% } %>
</select>

<input type="submit" value="Update News Source">


<hr>

<input type="button" onClick="doUpdateTheme()" value="Click to Update Theme!">
<input type="hidden" name="updateTheme" value="false">

</form>

<hr>

<a href="configPlugin.jsp">Config NewsSource plugins</a>

</body>
</html>